/**
 *	@file	telseq_grandma.c
 *	@brief	育て屋ばあさん　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "savedata/sodateyadata.h"
#include "field/sodateya.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_grandma.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

/**
 *	@brief	電話　育て屋ばあさんの会話抽選
 */
u16 PMTelPlayL_Grandma(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	int i;
	SODATEYA_WORK* sp;
	SODATEYA_POKE* pp;

	wk->seq_code = TEL_SEQ_SIMPLE;

	//着信はない

	//同一マップにいるか？
	if(wk->pbk->map_id == man->zone_id)	{
		return TEL_TK_GRANDMA_NE;
	}

	sp = SaveData_GetSodateyaWork(man->save);
	wk->tmp_wk = (void*)sp;

	wk->tmp_u8 = 0;
	for(i = 0;i < SODATEYA_POKE_MAX;i++){
		pp = SodateyaWork_GetSodateyaPokePointer( sp, i);

#if GS_AFTERMASTER_GF1044_20090929_FIX
		if(PokePasoParaGet(SodateyaPoke_GetPokePasoPointer(pp), ID_PARA_monsno, NULL)){
			wk->tmp_u8++;
		  wk->tmp_buf[i] = GetSodatePokeLevel(pp);
		}else{
      wk->tmp_buf[i] = 0;
    }
#else
		if(PokePasoParaGet(SodateyaPoke_GetPokePasoPointer(pp), ID_PARA_monsno, NULL)){
			wk->tmp_u8++;
		}
		wk->tmp_buf[i] = GetSodatePokeLevel(pp);
#endif
	}
	wk->seq_code = TEL_SEQ_GRANDMA;
	return 0;
}

/**
 *	@brief	育てばあさんの会話シーケンス
 */
int PMTelSeq_TypeGrandma(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		//挨拶出力
		PMTel_MsgPrintGmmSex(man,man->m_person,m_grandma_10_m,m_grandma_10_f);
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		if(wk->tmp_u8 == 0){
			//立ち寄るがいい
			PMTel_MsgPrintGmm(man,man->m_person,m_grandma_16);
			wk->seq = 0xFF;
			return FALSE;
		}
		//げんきじゃぞ
		PMTel_MsgPrintGmm(man,man->m_person,m_grandma_11);
		break;
	case 2:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		{
			SODATEYA_POKE* pp;
			u8 idx = 0;

			if(wk->tmp_buf[0]){
				idx = 0;
				if(wk->tmp_buf[1] == 0){
					wk->seq = 4;	//一匹しかいない
				}else{
					wk->seq = 3;	//二匹いる
				}
			}else if(wk->tmp_buf[1]){
				idx = 1;
#if GS_AFTERMASTER_GF1044_20090929_FIX
				wk->seq = 4;
#else
				wk->seq = 3;
#endif
			}else{
				//様子を見においで
				PMTel_MsgPrintGmm(man,man->m_person,m_grandma_15);
				wk->seq = 0xFF;	//最後へ
				return FALSE;
			}
			pp = SodateyaWork_GetSodateyaPokePointer((SODATEYA_WORK*)wk->tmp_wk, idx);	//ポケモンの名前をセット
			WORDSET_RegisterPokeNickName( man->wset,
				WORDSET_TEL_LOCAL_BUF+0,
				SodateyaPoke_GetPokePasoPointer(pp));	//ポケモンの名前をセット
			WORDSET_RegisterNumber( man->wset, WORDSET_TEL_LOCAL_BUF+1,
				wk->tmp_buf[idx],3, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );

			//レベルで言うと…
			PMTel_MsgPrintGmm(man,man->m_person,m_grandma_12);
		}
		return FALSE;
	case 3:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//2匹目の様子
		{
			SODATEYA_POKE* pp;
			pp = SodateyaWork_GetSodateyaPokePointer((SODATEYA_WORK*)wk->tmp_wk, 1);	//ポケモンの名前をセット
			WORDSET_RegisterPokeNickName( man->wset,
				WORDSET_TEL_LOCAL_BUF+0,
				SodateyaPoke_GetPokePasoPointer(pp));	//ポケモンの名前をセット
			WORDSET_RegisterNumber( man->wset, WORDSET_TEL_LOCAL_BUF+1,
				wk->tmp_buf[1],3, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
		}
		//レベルで言うと…
		PMTel_MsgPrintGmm(man,man->m_person,m_grandma_12);
		break;
	case 4:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		if(wk->tmp_u8 == 1){	//もう一匹預かれるよ
			PMTel_MsgPrintGmm(man,man->m_person,m_grandma_14);
		}else{
			PMTel_MsgPrintGmm(man,man->m_person,m_grandma_15);
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}
